package com.erp.common.web;

import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.erp.app.common.ProjectConstants;
import com.erp.framework.controller.action.SimpleUserStyleAction;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class UploadFileAction extends SimpleUserStyleAction {
    private static final String functionId = "uploadFile.do";

    public static final String actionUpload = "upload";
    public static final String actionDisplay = "display";

    protected String getModuleName(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response){
        return "";
    }
    
    public ActionForward otherExecute(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response) throws ServletException {
        HttpSession session = request.getSession(false);
        ActionErrors errors = new ActionErrors();
        

        UploadFileForm form = (UploadFileForm) formIn;
        String action = form.getPageEvent();
        action = (action == null) ? "" : action.trim();
        ActionForward actionForward = new ActionForward(mapping.getInput());

        boolean handled = false;

        HashMap params = (HashMap) session.getAttribute(ProjectConstants.SESSION_UPLOAD_FILE_PARAM);
        session.removeAttribute(ProjectConstants.SESSION_UPLOAD_FILE_PARAM);

        if (params != null) {
            form.setParams(params);
            handled = true;
        }

        // Action: Upload a file
        if (!handled && action.equalsIgnoreCase(actionUpload)) {
            handled = true;

            try {
                FormFile file = form.getTheFile();

                if (!CommonUtil.isEmpty(file.getContentType())) {

                    HashMap params2 = form.getParams();
                    String sessionValueName = (String) params2.get(ProjectConstants.SESSION_UPLOAD_FILE_SESSION_NAME);
                    String sessionValue = (String) params2.get(ProjectConstants.SESSION_UPLOAD_FILE_SESSION_VALUE);
                    HashMap otherParams = (HashMap) params2.get(ProjectConstants.SESSION_OTHER_PARAMS);

                    params2.put(ProjectConstants.SESSION_UPLOAD_FILE_RETURN_FORM_FILE, form.getTheFile());
                    session.setAttribute(ProjectConstants.SESSION_UPLOAD_FILE_RETURN_PARAM, params2);
                    session.setAttribute(sessionValueName, sessionValue);

                    if (otherParams != null) {
                        session.setAttribute(ProjectConstants.SESSION_OTHER_PARAMS, otherParams);
                    }
                }
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
        }

        // Action: display the image
        if (!handled && action.equalsIgnoreCase(actionDisplay)) {
            try {
                if (form.getContent() != null) {
                    OutputStream out = response.getOutputStream();
                    response.setContentType(form.getContentType());
                    response.setStatus(response.SC_OK);
                    byte[] content = form.getContent();
                    out.write(content);
                    out.close();
                    return null;
                }
            } catch (Exception e) {
            	Logger.logExceptions(this, e);
            }
            handled = true;
        }

        saveErrors(request, errors);
        return actionForward;
    }
}
